﻿using Chaotic.DataBase.DBAccessLibrary.Model;
using Chaotic.Module.DBTool.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using Telerik.Windows.Diagrams.Core;

namespace Chaotic.Module.DBTool.Views
{
    /// <summary>
    /// DBTreeContent.xaml 的交互逻辑
    /// </summary>
    public partial class DBTreeContent : UserControl
    {
        public DBTreeContent()
        {
            InitializeComponent();
            this.Loaded += DBTreeContent_Loaded;
        }

        private void DBTreeContent_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.DataContext is DBTreeContentViewModel viewModel)
                viewModel.LoadData();
        }

        private void dbtreeview_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            // 获取鼠标位置下的元素
            var hit = dbtreeview.InputHitTest(e.GetPosition(dbtreeview)) as DependencyObject;

            // 向上查找 TreeViewItem
            while (hit != null && !(hit is RadTreeViewItem))
            {
                hit = VisualTreeHelper.GetParent(hit);
            }

            var treeViewItem = hit as RadTreeViewItem;
            if (treeViewItem != null)
            {
                treeViewItem.IsSelected = true;
                treeViewItem.Focus();
                e.Handled = true;
            }
        }

        Point _lastMouseDownPoint;
        private void dbtreeview_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if(e.LeftButton == MouseButtonState.Pressed )
            {
                Point currentPoint = e.GetPosition(dbtreeview);

                if (Math.Abs(currentPoint.X - _lastMouseDownPoint.X) > 10.0 ||
                   Math.Abs(currentPoint.Y - _lastMouseDownPoint.Y) > 10.0)
                {
                    // 获取鼠标位置下的元素
                    var hit = dbtreeview.InputHitTest(e.GetPosition(dbtreeview)) as DependencyObject;

                    // 向上查找 TreeViewItem
                    while (hit != null && !(hit is RadTreeViewItem))
                    {
                        hit = VisualTreeHelper.GetParent(hit);
                    }

                    var treeViewItem = hit as RadTreeViewItem;
                    if (treeViewItem != null)
                    {
                        if (treeViewItem.DataContext is TableEntity table)
                        {
                            DragDrop.DoDragDrop(dbtreeview, table.TransName, DragDropEffects.Move);
                        }
                    }
                }
            }
        }

        private void dbtreeview_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if(e.ChangedButton == MouseButton.Left)
            {
                _lastMouseDownPoint = e.GetPosition(dbtreeview);
            }
        }
    }
}
